package com.kalua.list.dbaccess.retailer;

import static com.kalua.list.dbaccess.DBUtils.getInstantFrom;
import static com.kalua.list.dbaccess.DBUtils.getNumericIdFrom;
import static com.kalua.list.dbaccess.retailer.RetailerRowMapper.RETAILER_ID_COL_NAME;

import java.time.Instant;

import org.springframework.cassandra.core.RowMapper;

import com.datastax.driver.core.Row;
import com.datastax.driver.core.exceptions.DriverException;
import com.kalua.list.domainmodel.NumericId;
import com.kalua.list.domainmodel.TextIdentifier;
import com.kalua.list.domainmodel.retailer.CrawlBatchInfo;
import com.kalua.list.domainmodel.retailer.CrawlBatchInfoImpl;

public class CrawlBatchInfoRowMapper implements RowMapper<CrawlBatchInfo>{

	public static final String CRAWL_BATCH_INFO_COL_FAMILY = "crawl_batch_info";
	
	public static final String BATCH_ID_COL_NAME="batch_id";
	public static final String PREVIOUS_BATCH_ID_COL_NAME = "previous_batch_id";
	public static final String PRODUCT_COUNT_COL_NAME = "product_count";
	public static final String UPDATE_COUNT_COL_NAME = "update_count";
	public static final String CREATE_COUNT_COL_NAME = "create_count";
	public static final String INTERNAL_FAILURE_COUNT_COL_NAME = "internal_failure_count";
	public static final String NON_VIABLE_COUNT_COL_NAME = "non_viable_count";
	public static final String TIME_COMPLETED_COL_NAME = "time_completed";
	public static final String TIME_STARTED_COL_NAME = "time_started";
 
	public static final String IS_LATEST_COL_NAME = "latest";
	
	@Override
	public CrawlBatchInfo mapRow(	Row row, int rowNum)
																					throws DriverException {
		CrawlBatchInfoImpl result;
		
		NumericId id = new NumericId(row.getLong(BATCH_ID_COL_NAME));		
		NumericId previousBatchId = getNumericIdFrom(row, PREVIOUS_BATCH_ID_COL_NAME);
		TextIdentifier retailerId = new TextIdentifier(row.getString(RETAILER_ID_COL_NAME));				
		Instant timeStarted = getInstantFrom(row, TIME_STARTED_COL_NAME);
		Instant timeCompleted = getInstantFrom(row, TIME_COMPLETED_COL_NAME);
		
		result = new CrawlBatchInfoImpl(	id, previousBatchId, retailerId, timeStarted);
		result.setTimeCompleted(timeCompleted);
		result.setNumberOfProductsExtracted(row.getInt(PRODUCT_COUNT_COL_NAME));
		result.setUpdateCount(row.getInt(UPDATE_COUNT_COL_NAME));
		result.setCreateCount(row.getInt(CREATE_COUNT_COL_NAME));
		result.setInternalFailureCount(row.getInt(INTERNAL_FAILURE_COUNT_COL_NAME));
		result.setNonViableCount(row.getInt(NON_VIABLE_COUNT_COL_NAME));
		
		return result;
	}
}